<?php /* CALENDAR $Id: vw_day_events.php,v 1.11 2005/03/16 01:12:41 gregorerhardt Exp $ */
global $this_day, $first_time, $last_time, $company_id, $event_filter, $event_filter_list, $AppUI;

// load the event types
$types = dPgetSysVal( 'EventType' );
$links = array();

$perms =& $AppUI->acl();
$user_id = $AppUI->user_id;
$other_users = false;
$no_modify = false;

if ($perms->checkModule("admin", "view")) {
	$other_users = true;
	if (($show_uid = dPgetParam($_REQUEST, "show_user_events", 0)) != 0) {
		$user_id = $show_uid;
		$no_modify = true;
		$AppUI->setState("event_user_id", $user_id);
	}
}

// assemble the links for the events
$events = CEvent::getEventsForPeriod( $first_time, $last_time, $event_filter, $user_id );
$events2 = array();

$start_hour = dPgetConfig('cal_day_start');
$end_hour   = dPgetConfig('cal_day_end');

foreach ($events as $row) {
	$start = new CDate( $row['event_start_date'] );
	$end = new CDate( $row['event_end_date'] );

	$events2[$start->format( "%H%M%S" )][] = $row;

	if ($start_hour > $start->format ("%H")) {
	    $start_hour = $start->format ("%H");
	}
	if ($end_hour < $end->format("%H")) {
	    $end_hour = $end->format("%H");
	}
// the link
/*
	$link['href'] = "?m=calendar&a=view&event_id=".$row['event_id'];
	$link['alt'] = $row['event_description'];
	$link['text'] = '<img src="./images/obj/event.gif" width="16" height="16" border="0" alt="" />'
		.'<span class="event">'.$row['event_title'].'</span>';
	$links[$start->format( FMT_TIMESTAMP_DATE )][] = $link;
*/
}

$tf = $AppUI->getPref('TIMEFORMAT');

$dayStamp = $this_day->format( FMT_TIMESTAMP_DATE );

$start = $start_hour;
$end = $end_hour;
$inc = dPgetConfig('cal_day_increment');

if ($start === null ) $start = 8;
if ($end === null ) $end = 17;
if ($inc === null) $inc = 15;


$this_day->setTime( $start, 0, 0 );

$html  = '<form action="'.$_SERVER['REQUEST_URI'].' method="post" name="pickFilter">'.
		 '<input type="hidden" name="tab" value="0"/>'.
		 '<table cellspacing="0" cellpadding="3" border="0" width="100%" class="infopanel">'.
		 '<tr><td valign="middle">'.
			 '<table border="0" cellpadding="0" cellspacing="2" style="height: 100%">'.
			 '<tr style="height: 100%"><td valign="middle">'.
					$AppUI->_('View').':</td><td>' .
				arraySelect($event_filter_list, 'event_filter',
					'onChange="document.pickFilter.submit()" class="text"', $event_filter, true ).
			 '</td></tr></table>';
if ($other_users) {
	$html .= $AppUI->_("Show Events for") . ":" . "<select name='show_user_events' onchange='document.pickFilter.submit()' class='text'>";
	$q  = new DBQuery;
	$q->addTable('users', 'u');
	$q->addTable('contacts', 'con');
	$q->addQuery('user_id, user_username, contact_first_name, contact_last_name');
	$q->addWhere("user_contact = contact_id");

	if (($rows = $q->loadList())) {
		foreach ($rows as $row)
		{
			if ( $user_id == $row['user_id'])
				$html .= '<OPTION VALUE="'.$row['user_id'].'" SELECTED>'.$row['user_username'];
			else
				$html .= '<OPTION VALUE="'.$row['user_id'].'">'.$row['user_username'];
		}
	}
	$html .= '</select>';

}
$html .= '</form></td></tr></table>';
$html .= '<table cellspacing="0" cellpadding="3" border="0" width="100%" class="list">';
$rows = 0;
for ($i=0, $n=($end-$start)*60/$inc; $i < $n; $i++) {
	$min = $this_day->getMinute();
	$html .= '<tr class="'.($min ? 'second' : 'first').'">';

	$tm = $this_day->format( $min ? '%M' : $tf );
	$html .= '<td width="1%" align="right" nowrap="nowrap">'.($min ? $tm : "<b>$tm</b>").'</td>';

	$timeStamp = $this_day->format( "%H%M%S" );
	if( @$events2[$timeStamp] ) {
		$count = count($events2[$timeStamp]);
		for ($j = 0; $j < $count; $j++) {
			$row = $events2[$timeStamp][$j];

			$et = new CDate( $row['event_end_date'] );
			$rows = (($et->getHour()*60 + $et->getMinute()) - ($this_day->getHour()*60 + $this_day->getMinute()))/$inc;

			$href = '?m=calendar&a=view&event_id='.$row['event_id'];
			$alt = $row['event_description'];

			$html .= '<td class="event" rowspan="'.$rows.'" valign="top">';

			$link = ($href ? '<a href="'.$href.'" class="event" title="'.$alt.'">' : '').$row['event_title'].($href ? '</a>' : '');
			$html .= '<table cellspacing="0" cellpadding="0" border="0"><tr>';
			$html .= '<td class="internal"><img src="'.dPfindImage( 'event'.$row['event_type'].'.png', 'calendar' ).'">';
			//$html .= '</td><td class="internal">&nbsp;<b>' . $types[$row['event_type']] . '</b></td></tr></table>';
			$html .= '</td><td class="internal">&nbsp;<b>' . $link . '</b></td></tr></table>';

			/*
			$html .= $href ? '<a href="'.$href.'" class="event" title="'.$alt.'">' : '';
			$html .= $row['event_title'];
			$html .= $href ? '</a>' : '';
			*/
			$html .= '</td>';
		}
	} else {
		if (--$rows <= 0)
			$html .= "\n\t<td></td>";
	}

	$html .= "\n</tr>";

	$this_day->addSeconds( 60*$inc );
}


$html .= '</table>';
echo $html;
?>